<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <!-- 引入最新版本的jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 400px;
            margin: 50px auto;
            background: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h2 {
            text-align: center;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .btn {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            width: 100%;
        }
        .btn:hover {
            background-color: #45a049;
        }
        .message {
            margin: 15px 0;
            padding: 10px;
            border-radius: 3px;
            display: none;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
        .links {
            text-align: center;
            margin-top: 15px;
        }
        .loading {
            display: none;
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>用户登录</h2>
        
        <div id="message-success" class="message success"></div>
        <div id="message-error" class="message error"></div>
        
        <form id="login-form">
            <div class="form-group">
                <label for="username">用户名：</label>
                <input type="text" id="username" name="username" required>
            </div>
            
            <div class="form-group">
                <label for="password">密码：</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <div class="form-group">
                <button type="submit" class="btn">登录</button>
            </div>
            
            <div class="loading">
                登录中，请稍候...
            </div>
            
            <!-- 注册链接已移除 -->
        </form>
    </div>

    <script>
        $(document).ready(function() {
            // 检查登录状态
            checkLoginStatus();
            
            // 登录表单提交
            $('#login-form').on('submit', function(e) {
                e.preventDefault();
                
                // 隐藏之前的消息
                $('.message').hide();
                
                // 显示加载中
                $('.loading').show();
                
                // 获取表单数据
                var username = $('#username').val();
                var password = $('#password').val();
                
                // 验证表单
                if (!username || !password) {
                    showError('用户名和密码不能为空');
                    $('.loading').hide();
                    return;
                }
                
                // 发送AJAX请求
                $.ajax({
                    url: '/user/login',
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        username: username,
                        password: password
                    },
                    success: function(response) {
                        $('.loading').hide();
                        
                        if (response.code === 1) {
                            // 登录成功
                            showSuccess(response.msg || '登录成功');
                            
                            // 延迟跳转到Dify服务器列表页面
                            setTimeout(function() {
                                window.location.href = '/dify_server/list_page';
                            }, 1500);
                        } else {
                            // 登录失败
                            showError(response.msg || '登录失败');
                        }
                    },
                    error: function(xhr, status, error) {
                        $('.loading').hide();
                        showError('请求失败，请稍后再试');
                        console.error(xhr, status, error);
                    }
                });
            });
            
            // 显示成功消息
            function showSuccess(message) {
                $('#message-success').text(message).show();
            }
            
            // 显示错误消息
            function showError(message) {
                $('#message-error').text(message).show();
            }
            
            // 检查登录状态
            function checkLoginStatus() {
                $.ajax({
                    url: '/user/checkLogin',
                    type: 'GET',
                    dataType: 'json',
                    success: function(response) {
                        if (response.code === 1) {
                            // 已登录，跳转到首页
                            window.location.href = '/index.html';
                        }
                    }
                });
            }
        });
    </script>
</body>
</html>